package com.reone.jsbridgedemo.utils;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by wangxingsheng on 2018/7/13.
 */
public class Utils {

    public static String getAssetHtml(String html){
        String assetPath = "file:///android_asset/";
        String result = assetPath + html;
        LogUtils.d("getAssetHtml:"+result);
        return result;
    }

    public static String getTextFromEdit(EditText editText){
        String result = "null";
        if(editText != null && editText.getText() != null){
            result = editText.getText().toString();
        }
        LogUtils.d("getTextFromEdit:"+result);
        return result;
    }

    public static String getJsMethodInvokeName(String name,String... params){
        String jsPrefix = "javascript:";
        int count;
        StringBuffer sb = new StringBuffer();
        if(params != null && (count = params.length) > 0){
            for (int i = 0; i < count; i++) {
                if(i != 0){
                    sb.append(",");
                }
                sb.append("'").append(params[i]).append("'");
            }
        }
        String result = jsPrefix + name + "(" + sb.toString() + ")";
        LogUtils.d("getJsMethodInvokeName:"+result);
        return result;
    }

    public static TextView newTextView(Context context,@NonNull String text){
        TextView textView = new TextView(context);
        textView.setTextColor(Color.BLACK);
        textView.setText(text);
        return textView;
    }

    public static String result(String result) {
        if(TextUtils.isEmpty(result)){
            return "null";
        }
        return result.trim();
    }
}
